<?php
/**
 * Created by PhpStorm.
 * User: xuxiaoming
 * Date: 2017/11/7
 * Time: 17:55
 */

namespace App\Http\Controllers;


use App\Models\User;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller;

class UserController extends Controller
{
    /**
     * 列表
     * @return string
     */
    public function index()
    {
        $users = User::all();
        if ($users) {
            return sendJsonData($users->toArray());
        } else{
            return sendJsonMessage('数据不存在',true);
        }
    }


    /**
     * 列表
     * @param Request $request
     * @return string
     */
    public function getUser(Request $request, $id)
    {
        $user = User::find($id);
        if ($user) {
            return sendJsonData($user->toArray());
        } else{
            return sendJsonMessage('数据不存在',true);
        }
    }

    /**
     * 创建用户
     * @param Request $request
     * @return string
     */
    public function createUser(Request $request)
    {
        $users = User::create($request->all());
        return sendJsonData($users->toArray());
    }

    /**
     * 更新用户
     *
     * @param Request $request
     * @param $id
     * @return string
     */
    public function updateUser(Request $request, $id)
    {
        $users = User::find($id);
        $users->name = $request->input('name');
        $users->email = $request->input('email');
        $users->mobile = $request->input('mobile');
        $users->save();
        return sendJsonData($users->toArray());
    }

    /**
     * 删除用户
     *
     * @param $id
     * @return string
     */
    public function deleteUser($id)
    {
        $users = User::find($id);
        if ($users) {
            $users->delete();
            return sendJsonMessage('删除成功');
        } else{
            return sendJsonMessage('数据不存在',true);
        }

    }


}